package com.hitqz.robot.biz.flow.component.patrol;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolTicketsDto;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolItemVo;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.core.FileProperties;
import com.hitqz.robot.biz.service.PatrolItemService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.MediaFileDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.camera.dto.TakePicParamsDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Date;

@Slf4j
@LiteflowComponent(value = "TakePic", name = "[巡检]拍照")
@AllArgsConstructor
public class TakePic extends NodeComponent {

    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;

    private final PatrolItemService patrolItemService;

    private final FileProperties fileProperties;


    @Override
    public void process() throws Exception {
        FlowContextDto dto = this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolTicketsDto.class);
        log.info("{} ---params: {}", task.getParseOrderParams(), JSONObject.toJSONString(params));
        if (params.getDetectEquipmentId()==null){
            log.warn("没有检测设备信息,不执行");
            return;
        }
        RobotEquipment channel = robotEquipmentService.getById(params.getDetectEquipmentId());
        IEquipmentService service = equipmentFactory.getServiceByBrand(channel.getBrand());
        // /日期/巡检对象
        PatrolItemVo item = patrolItemService.getVoById(params.getPatrolItemId());
        String folder ="patrol" + File.separator + DateUtil.format(new Date(),"yyyyMMdd")+ File.separator + item.getPatrolObjectStr();
        //MediaFileDto mediaFileDto = service.takePic(channel.getIp(), channel.getChannel(), fileProperties.getLocal().getBasePath(), folder);
        TakePicParamsDto paramsDto = new TakePicParamsDto();
        paramsDto.setBasePath(fileProperties.getLocal().getBasePath());
        paramsDto.setFolder(folder);
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(channel.getPassword());
        queryParamsDto.setUsername(channel.getUsername());
        queryParamsDto.setPort(channel.getPort());
        queryParamsDto.setChannel(channel.getChannel());
        queryParamsDto.setIp(channel.getIp());
        MediaFileDto mediaFileDto = (MediaFileDto) service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.TAKE_PIC,paramsDto);
        log.info("拍照结果 {}", mediaFileDto);
        String relativePath = mediaFileDto.getRelativePath();
        params.setMediaUrl(relativePath);
        //结果传递到上下文参数
        task.setParseOrderParams(JSONObject.toJSONString(params));
    }
}